using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Mbs.Elements;
using Mbs.Globalization;
using Mbs.Modularity.AddIns;
using Mbs.ComponentModel.Container;

namespace Mbs.Windows.Elements.Builders
{
    [Component(Id = "ToolStrip.MenuItem", Service = typeof(IElementBuilder))]
    public class ToolStripMenuItemBuilder : ToolStripItemBuilder<ToolStripMenuItem>
    {
        protected override ToolStripMenuItem CreateItem(IAddInItem addInItem)
        {
            return new MbsToolStripMenuItem(AddInItem);
        }

        private class MbsToolStripMenuItem : ToolStripMenuItem, Mbs.Globalization.ISupportGlobalization, IAddInItemHost
        {
            public IAddInItem AddInItem { get; private set; }
            public MbsToolStripMenuItem(IAddInItem addInItem)
            {
                AddInItem = addInItem;
                IsSupportGlobalization = true;

                this.Initialize(addInItem);
                if (AddInItem.Properties.Contains("Shortcut"))
                    ShortcutKeys = ParseShortcut(AddInItem.Properties.Get<string>("Shortcut"));

                InitializeResource();
            }


            public bool IsSupportGlobalization { get; set; }

            public void InitializeResource()
            {
                Text = AddInItem.Label;
                Image = AddInItem.Properties.Contains("Icon") ? ResourceRepository.Images.Get(AddInItem.Properties.Get<string>("Icon")) : null;
                if (AddInItem.Properties.Contains("ToolTip"))
                    ToolTipText = StringParser.Parse(AddInItem.Properties.Get<string>("ToolTip"));
            }

            public void RefreshResource()
            {
                InitializeResource();
            }

            protected override void Dispose(bool disposing)
            {
                base.Dispose(disposing);
            }
        }

        internal static Keys ParseShortcut(string shortcutString)
        {
            Keys shortCut = Keys.None;
            if (shortcutString.Length > 0)
            {
                try
                {
                    foreach (string key in shortcutString.Split('|'))
                    {
                        shortCut |= (System.Windows.Forms.Keys)Enum.Parse(typeof(System.Windows.Forms.Keys), key);
                    }
                }
                catch (Exception ex)
                {
                    ex.RaiseException();
                    return System.Windows.Forms.Keys.None;
                }
            }
            return shortCut;
        }

        protected override void OnInitInstance(object caller, IAddInItem element, List<object> subItems, ToolStripItem instance)
        {
            var menuItem = instance as ToolStripMenuItem;
            if (menuItem != null)
            {
                //if (element.IsSite())
                    ElementService.Sites.RegisterSite(element.Path, menuItem);

                foreach (var o in subItems)
                    if (o is ToolStripItem)
                        ElementService.Sites[element.Path].Add(o, null);

                BindCommand(element, menuItem, "Click");
            }
        }

    }
}
